﻿package mortal.game.view.mainUI.smallMap.view
{
    import com.fyGame.fyMap.*;
    import com.gengine.resource.*;
    import com.gengine.resource.info.*;
    import com.mui.controls.*;
    import com.mui.utils.*;
    import flash.display.*;
    import flash.events.*;
    import mortal.common.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.*;
    import mortal.game.view.common.*;
    import mortal.mvc.core.*;

    public class SmallMapImage extends GSprite
    {
        private var _bitmapContainer:GSprite;
        private var _bitmap:GBitmap;
        private var _curMapId:int = -1;
        private var _loading:Boolean = false;
        private var _loadingFileName:String;
        private var _smallMapScale:Number = 0.5;
        private var _myWidth:int = 500;
        private var _myHeight:int = 500;
        private var _mapInfo:FyMapInfo;
        private var _callback:Function;
        private var _delayId:int;

        public function SmallMapImage()
        {
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._bitmapContainer.dispose(param1);
            this._bitmapContainer = null;
            this._bitmap.dispose(param1);
            this._bitmap = null;
            this._curMapId = -1;
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this.mouseEnabled = true;
            this.mouseChildren = true;
            this._bitmapContainer = UICompomentPool.getUICompoment(GSprite) as GSprite;
            this.addChild(this._bitmapContainer);
            this._bitmap = UIFactory.bitmap("", 0, 0, this._bitmapContainer);
            this._bitmapContainer.mouseEnabled = true;
            this._bitmapContainer.mouseChildren = true;
            this._bitmapContainer.doubleClickEnabled = true;
            this._bitmapContainer.configEventListener(MouseEvent.CLICK, this.clickHandler);
            this._bitmapContainer.configEventListener(MouseEvent.MOUSE_MOVE, this.moveHandler);
            this._bitmapContainer.configEventListener(MouseEvent.DOUBLE_CLICK, this.flyToHandler);
            return;
        }// end function

        public function get myWidth() : int
        {
            return this._myWidth;
        }// end function

        public function set myWidth(param1:int) : void
        {
            this._myWidth = param1;
            return;
        }// end function

        public function get myHeight() : int
        {
            return this._myHeight;
        }// end function

        public function set myHeight(param1:int) : void
        {
            this._myHeight = param1;
            return;
        }// end function

        public function get smallMapScale() : Number
        {
            return this._smallMapScale;
        }// end function

        public function load(param1:FyMapInfo, param2:Function) : void
        {
            var _loc_3:ResourceInfo = null;
            this._callback = param2;
            this._mapInfo = param1;
            if (this._mapInfo.mapId != this._curMapId)
            {
                if (this._loading)
                {
                    this.stop();
                }
                _loc_3 = SceneConfig.instance.getImageInfo(this._curMapId);
                if (_loc_3 && _loc_3.isLoaded)
                {
                    _loc_3.dispose();
                }
                this._curMapId = this.getShouldLoadMapId(this._mapInfo.mapId);
                this.loadMiniMap();
            }
            else if (!this._loading)
            {
                if (param2 != null)
                {
                    param2.apply();
                }
            }
            return;
        }// end function

        private function loadMiniMap() : void
        {
            var _loc_1:* = SceneConfig.instance.getImageInfo(this._curMapId);
            if (_loc_1)
            {
                this._loading = true;
                this._loadingFileName = _loc_1.name;
                LoaderManager.instance.loadInfo(_loc_1, this.onLoaded);
            }
            return;
        }// end function

        public function stop() : void
        {
            this._loading = false;
            this._loadingFileName = "";
            LoaderManager.instance.removeResourceEvent(this._loadingFileName, this.onLoaded);
            return;
        }// end function

        private function onLoaded(param1:MapImgInfo = null) : void
        {
            this._loading = false;
            this._loadingFileName = "";
            if (param1 == null || param1.mapID != this._curMapId)
            {
                if (this._callback != null)
                {
                    this._callback.apply();
                }
                return;
            }
            var _loc_2:* = param1.bitmapData;
            this._smallMapScale = DisplayUtil.calculateFixedScale(this._myWidth, this._myHeight, _loc_2.width, _loc_2.height);
            this._bitmap.bitmapData = _loc_2;
            this._bitmap.scaleX = this._smallMapScale;
            this._bitmap.scaleY = this._smallMapScale;
            this._bitmap.x = (500 - this._bitmap.width) / 2;
            this._bitmap.y = (500 - this._bitmap.height) / 2;
            if (this._callback != null)
            {
                this._callback.apply();
            }
            return;
        }// end function

        public function get bWidth() : int
        {
            return this._bitmap.width;
        }// end function

        public function get bHeight() : int
        {
            return this._bitmap.height;
        }// end function

        public function get dx() : int
        {
            return this._bitmap.x;
        }// end function

        public function get dy() : int
        {
            return this._bitmap.y;
        }// end function

        private function getShouldLoadMapId(param1:int) : int
        {
            var _loc_2:* = GameMapConfig.instance.getMapInfo(param1);
            if (_loc_2 == null)
            {
                Alert.show("求策划配置gameMap.xml, 地图id=" + param1);
                return param1;
            }
            return _loc_2.mapscene;
        }// end function

        private function clickHandler(event:MouseEvent) : void
        {
            var _loc_2:* = this._bitmap.mouseX * this._smallMapScale * (this._mapInfo.gridWidth / this._bitmap.width);
            var _loc_3:* = this._bitmap.mouseY * this._smallMapScale * (this._mapInfo.gridHeight / this._bitmap.height);
            Dispatcher.dispatchEvent(new DataEvent(EventName.SmallMapClick, {x:_loc_2, y:_loc_3, mapInfo:this._mapInfo}));
            return;
        }// end function

        private function flyToHandler(event:MouseEvent) : void
        {
            var _loc_2:* = this._bitmap.mouseX * this._smallMapScale * (this._mapInfo.gridWidth / this._bitmap.width);
            var _loc_3:* = this._bitmap.mouseY * this._smallMapScale * (this._mapInfo.gridHeight / this._bitmap.height);
            Dispatcher.dispatchEvent(new DataEvent(EventName.SmallMapDoubleClick, {x:_loc_2, y:_loc_3, mapInfo:this._mapInfo}));
            return;
        }// end function

        private function moveHandler(event:MouseEvent) : void
        {
            var _loc_2:* = this._bitmap.mouseX * this._smallMapScale * (this._mapInfo.gridWidth / this._bitmap.width);
            var _loc_3:* = this._bitmap.mouseY * this._smallMapScale * (this._mapInfo.gridHeight / this._bitmap.height);
            Dispatcher.dispatchEvent(new DataEvent(EventName.SmallMapShowCurPoint, {x:_loc_2, y:_loc_3}));
            return;
        }// end function

    }
}
